package adminuser

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/code"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/userServiceClient/userrpc"
	"github.com/zeromicro/go-zero/core/logc"
	"github.com/zeromicro/go-zero/core/logx"

	v1 "gitee.com/liuxuezhan/ar-platform/rpc-client/authRoleClient/api/role/v1"
)

type AdminUserSearchNoRoleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 查询不为某角色管理员用户
func NewAdminUserSearchNoRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminUserSearchNoRoleLogic {
	return &AdminUserSearchNoRoleLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AdminUserSearchNoRoleLogic) AdminUserSearchNoRole(req *types.AdminUserSearchNoRoleRequest) (resp *types.AdminUserSearchNoRoleResponse, err error) {
	resp = &types.AdminUserSearchNoRoleResponse{}
	user_list := make([]*types.AdminUserInfo, 0)

	if req.Role == 0 {
		return nil, code.ParamsInvalid
	}

	// 首先调 roles
	getRolesAccountIdsResp, err := l.svcCtx.AuthRoleRpc.GetRolesAccountIds(l.ctx, &v1.GetRolesAccountIdsRequest{
		RoleIds:     []int64{int64(req.Role)},
		AccountType: v1.AccountType_User,
	})
	if err != nil {
		logc.Error(l.ctx, "GetRolesAccountIds err: ", err)
		return nil, err
	}

	// 然后拿 roles 返回的 ids 调接口
	adminUserListNotInIdsResp, err := l.svcCtx.UserServiceRPC.AdminUserListNotInIds(l.ctx, &userrpc.AdminUserListNotInIdsRequest{
		AccountKeyword: req.AccountKeyword,
		IdList:         getRolesAccountIdsResp.AccountIds,
		Phone:          req.Phone,
		Page:           req.Page,
		PageSize:       req.PageSize,
	})
	if err != nil {
		logc.Error(l.ctx, "AdminUserListNotInIds err: ", err)
		return nil, err
	}

	roleListResp, err := l.svcCtx.AuthRoleRpc.GetRoleList(l.ctx, &v1.GetRoleListRequest{
		Page:     0,
		PageSize: 100,
		Status:   1,
	})
	if err != nil {
		logc.Error(l.ctx, "GetRoleList err: ", err)
		return nil, err
	}

	for _, user := range adminUserListNotInIdsResp.AdminUserList {

		user := &types.AdminUserInfo{
			Id:              user.Id,
			Account:         user.Account,
			Password:        user.Password,
			Phone:           user.Phone,
			PhoneCode:       user.PhoneCode,
			Nickname:        user.Nickname,
			Email:           user.Email,
			Role:            user.Role,
			RegisterAt:      user.RegisterAt,
			LoginAt:         user.LoginAt,
			LoginIp:         user.LoginIp,
			LoginNumber:     user.LoginNumber,
			UserStatus:      user.UserStatus,
			LoginStatus:     user.LoginStatus,
			GoogleCode:      user.GoogleCode,
			IsOpenGoogle:    user.IsOpenGoogle,
			IsBindingGoogle: user.IsBindingGoogle,
			HeadPictureUrl:  user.HeadPictureUrl,
		}
		for _, role := range roleListResp.List {
			if role.Id == int64(user.Role) {
				user.RoleName = role.RoleName
			}
		}
		user_list = append(user_list, user)
	}

	resp.AdminUserList = user_list
	resp.Pagination = types.Pagination{
		Page:      adminUserListNotInIdsResp.Pagination.Page,
		PageSize:  adminUserListNotInIdsResp.Pagination.PageSize,
		Total:     adminUserListNotInIdsResp.Pagination.Total,
		TotalPage: adminUserListNotInIdsResp.Pagination.TotalPage,
	}

	return
}
